package com.tcxhb.job.agent.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tcxhb.job.agent.constants.AgentErrorCode;
import com.tcxhb.job.agent.constants.BestJobConfig;
import com.tcxhb.job.agent.request.CallbackRequest;
import com.tcxhb.job.agent.service.JobServerApi;
import com.tcxhb.job.common.constants.JobApiConstants;
import com.tcxhb.job.common.model.MiscResult;
import com.tcxhb.job.common.service.HttpService;
import com.tcxhb.job.common.service.impl.HttpServiceImpl;
import com.tcxhb.job.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 服务器的请求服务
 * @Auther: tcxhb
 * @Date: 2023/11/5
 */
@Service
@Slf4j
public class JobServerApiImpl implements JobServerApi {
    @Autowired
    private BestJobConfig agentConfig;
    public HttpService httpService;

    public JobServerApiImpl() {
        HttpClient client = HttpClients.createDefault();
        httpService = new HttpServiceImpl(client);
    }

    @Override
    public MiscResult<Boolean> register(String ip, Integer port, String appName) {
        try {
            JSONObject params = new JSONObject();
            params.put("host", ip);
            params.put("port", port);
            params.put("appName", appName);
            String url = getUrl(JobApiConstants.api_beat);
            String str = httpService.get(url, params, null);
            log.info("agent-register:{}", str);
            return JsonUtils.toMiscBean(str, Boolean.class);
        } catch (Exception e) {
            log.error("agent-register exception:" + appName, e);
            throw new RuntimeException(AgentErrorCode.AGENT_HTTP_TO_SERVER_FAIL.getCode(), e);
        }
    }

    @Override
    public MiscResult<Boolean> offline(String ip, Integer port, String appName) {
        try {
            JSONObject params = new JSONObject();
            params.put("host", ip);
            params.put("port", port);
            params.put("appName", appName);
            String url = getUrl(JobApiConstants.api_offline);
            String str = httpService.get(url, params, null);
            return JsonUtils.toMiscBean(str, Boolean.class);
        } catch (Exception e) {
            log.error("job-register exception:" + appName);
            return MiscResult.err(AgentErrorCode.AGENT_HTTP_TO_SERVER_FAIL);
        }
    }

    @Override
    public MiscResult<Boolean> callback(CallbackRequest request) {
        try {
            JSONObject params = JsonUtils.beanToJson(request);
            String url = getUrl(JobApiConstants.api_callback);
            String str = httpService.get(url, params, null);
            return JsonUtils.toBean(str, MiscResult.class, Boolean.class);
        } catch (Exception e) {
            log.error("callback-http-exp:" + request.getInstanceId());
            return MiscResult.err(AgentErrorCode.AGENT_HTTP_TO_SERVER_FAIL);
        }
    }

    private String getUrl(String path) {
        return agentConfig.getServerHost() + path;
    }
}
